
package org.androidesk.guangdiananalytic.user;

import java.io.IOException;
import java.util.Calendar;

import org.androidesk.guangdiananalytic.MeasurementPeriod;
import org.androidesk.guangdiananalytic.TaskBase;
import org.androidesk.guangdiananalytic.util.DatabaseUtil;
import org.androidesk.guangdiananalytic.util.TimeUtil;
import org.apache.hadoop.conf.Configuration;
import org.apache.hadoop.hbase.client.Put;
import org.apache.hadoop.hbase.client.Result;
import org.apache.hadoop.hbase.client.Scan;
import org.apache.hadoop.hbase.filter.CompareFilter;
import org.apache.hadoop.hbase.filter.Filter;
import org.apache.hadoop.hbase.filter.FilterList;
import org.apache.hadoop.hbase.filter.SingleColumnValueFilter;
import org.apache.hadoop.hbase.io.ImmutableBytesWritable;
import org.apache.hadoop.hbase.mapreduce.TableMapReduceUtil;
import org.apache.hadoop.hbase.mapreduce.TableMapper;
import org.apache.hadoop.hbase.mapreduce.TableReducer;
import org.apache.hadoop.hbase.util.Bytes;
import org.apache.hadoop.io.IntWritable;
import org.apache.hadoop.mapreduce.Job;

    /**
    * Created by xiaochaihu on 14-7-4.
    * 统计某段时间内的新增用用户
    * @author xiaochaihu
    * @version 2.0
    */
    public class NewUserList_v2 extends TaskBase{
     /**
      * @string sourceTable 源数据表名
      */
     protected String sourceTable;
     /**
      * @string targetTable 结果数据表名
      */
     protected String targetTable;
     /**
      * @string targetCF 列族名
      */
     protected String targetCF;

     public NewUserList_v2(){
         this.sourceTable = "androidesk.user";
         this.targetTable = "rs.user.new";
         this.targetCF    = "c";
             
     }

     public NewUserList_v2(String sourceTable, String targetTable, String targetCF){
         this.sourceTable = sourceTable;
         this.targetTable = targetTable;
         this.targetCF    = targetCF;
             
     }

     private boolean checkTimeStamp(long beginTime, long endTime){
         if(beginTime >= endTime){
            return false;         
         }

         // flag 标记时间的合法性
         boolean flag = false;

         Calendar c = Calendar.getInstance();

         c.set(c.get(Calendar.YEAR) - 1, c.get(Calendar.MONTH), c.get(Calendar.DATE), 0, 0, 0);
         long oneYearsAgo = c.getTimeInMillis();
         oneYearsAgo = (long)(oneYearsAgo / 1000.0) * 1000;

         if(beginTime >= oneYearsAgo){
            flag = true;
                     
         }

        return flag;
             
     }

     public void countNewUsers(long beginTime, long endTime){
                 //  检测时间的合法性
         if(checkTimeStamp(beginTime, endTime)){
            return;
                     
         }
        //  开始统计工作
             
     }

     public static void main(String[] args){
         //TODO
         Calendar c = Calendar.getInstance();
         c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE) - 1, 0, 0, 0);
         long beginTime = c.getTimeInMillis();
         beginTime = (long)(beginTime / 1000.0) * 1000;

         c.set(c.get(Calendar.YEAR), c.get(Calendar.MONTH), c.get(Calendar.DATE), 0, 0, 0);
         long endTime = c.getTimeInMillis();
         endTime = (long)(endTime / 1000.0) * 1000;

         NewUserList_v2 newUserList_v2 = new NewUserList_v2();

         newUserList_v2.countNewUsers(beginTime, endTime);
             
     }

}
